/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package design;

import code.ValidateTokens;
import com.alee.laf.WebLookAndFeel;
import com.restfb.Connection;
import com.restfb.FacebookClient;
import com.restfb.types.Album;
import com.restfb.types.FriendList;
import easyJSON.easyJSON;
import java.awt.Color;
import java.awt.Toolkit;
import java.io.File;
import java.net.URLDecoder;
import javax.swing.JOptionPane;

/**
 *
 * @author Yuvraj Singh Babrah
 */
public class FetchTokens extends javax.swing.JFrame {
    /* properties */
    private String name = null;
    private FacebookClient facebookClient = null;
    private Connection<FriendList> friendsList = null;
    private Connection<Album> albumsList = null;
    public static boolean flag = false;

    /**
     * Creates new form fetchTokens
     */
    public FetchTokens() {
        int Result = checkFiles();
        if(Result == 0){
            JOptionPane.showMessageDialog(null, "lib folder missing! Please download the application again.", "Folder Missing", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
        splash();
        initComponents();
        design();
    }
    
    public int checkFiles(){
        String file_path = getClass().getProtectionDomain().getCodeSource().getLocation().toString();
        file_path = file_path.substring(file_path.indexOf("file:/") + 6, file_path.length());
        File check_file = new File(file_path);        
        String parent_name = check_file.getParentFile().getName();
        String parent_path = file_path.substring(0, file_path.indexOf(parent_name) + parent_name.length());
        try{
            parent_path = URLDecoder.decode(parent_path, "UTF-8");
        }
        catch(Exception e){}
        File check_dir = new File(parent_path + "/lib/");
        if(check_dir.isDirectory())
            return 1;
        return 0;
    }
    
    public void splash(){
        new Splash();
    }
    
    /**
     * custom design
     */
    public void design(){
        this.getContentPane().setBackground(new Color(59,89,152));
        this.setLocationRelativeTo(null);
        try{
            this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/icon_32.png")));
        }
        catch(Exception e){ }
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        tokenValue = new javax.swing.JTextField();
        validationStatus = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Welcome!! Download albums from Facebook with ease");
        setBackground(new java.awt.Color(59, 89, 152));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("enter the token value obtained via facebook app");

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("All Set");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        tokenValue.setPreferredSize(new java.awt.Dimension(454, 25));

        validationStatus.setFont(new java.awt.Font("Calibri", 1, 12)); // NOI18N
        validationStatus.setForeground(new java.awt.Color(255, 255, 255));
        validationStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        validationStatus.setText("waiting for  launch");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(205, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tokenValue, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(206, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(118, 118, 118)
                .addComponent(validationStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 629, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(160, 160, 160)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tokenValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(49, 49, 49)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(validationStatus)
                .addContainerGap(210, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // exit
        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // fetch token value and validate
        //JOptionPane.showMessageDialog(null, "kajsdk");
        //new Skeleton();
        String tokens = tokenValue.getText();
        if(tokens == null || tokens.length() == 0){
            validationStatus.setText("woops!! the token value cannot be empty");
        }
        else{
            if(!flag){
                flag = true;
            }
            else{
                return;
            }
            validationStatus.setText("working ...");
            new ValidateTokens(tokens, this, validationStatus).start(); // thread
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Setting LookAndFeel */
        try {
            WebLookAndFeel.install();
        } catch (Exception ex) {}

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FetchTokens().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField tokenValue;
    private javax.swing.JLabel validationStatus;
    // End of variables declaration//GEN-END:variables
}
